package com.ruoyi.web.controller.interview;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.interview.domain.IntervTalent;
import com.ruoyi.interview.service.IIntervTalentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author xuqinglong
 * @date 2022-11-10
 */
@RestController
@RequestMapping("/interview/talent")
public class IntervTalentController extends BaseController
{
    @Autowired
    private IIntervTalentService intervTalentService;


    @Autowired
    private ServerConfig serverConfig;



    /**
     * 查询人才列表
     */
    @PreAuthorize("@ss.hasPermi('system:talent:list')")
    @GetMapping("/list")
    public TableDataInfo list(IntervTalent intervTalent)
    {
        startPage();
        List<IntervTalent> list = intervTalentService.selectIntervTalentList(intervTalent);
        return getDataTable(list);
    }

    /**
     * 导出人才列表
     */
    @PreAuthorize("@ss.hasPermi('system:talent:export')")
    @Log(title = "导出人才信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntervTalent intervTalent)
    {
        List<IntervTalent> list = intervTalentService.selectIntervTalentList(intervTalent);
        ExcelUtil<IntervTalent> util = new ExcelUtil<IntervTalent>(IntervTalent.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 单个查询 通过id获取人才详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:talent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(intervTalentService.selectIntervTalentById(id));
    }

    /**
     * 新增人才
     */
    @PreAuthorize("@ss.hasPermi('system:talent:add')")
    @Log(title = "新增人才信息不包括简历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntervTalent intervTalent)
    {
        return toAjax(intervTalentService.insertIntervTalent(intervTalent));
    }

    /**
     * 修改人才
     */
    @PreAuthorize("@ss.hasPermi('system:talent:edit')")
    @Log(title = "修改人才信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntervTalent intervTalent)
    {
        return toAjax(intervTalentService.updateIntervTalent(intervTalent));
    }

    /**
     * 删除人才
     */
    @PreAuthorize("@ss.hasPermi('system:talent:remove')")
    @Log(title = "删除人才信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(intervTalentService.deleteIntervTalentByIds(ids));
    }



    /** 简历文件上传接口 */
    @PreAuthorize("@ss.hasPermi('system:talent:upload')")
    @Log(title = "简历文件上传接口", businessType = BusinessType.OTHER)
    @PostMapping("/uploadResume/{id}")
    /** 参数为前端传过来的obj对象*/
    public AjaxResult uploadResume(MultipartFile file , @PathVariable("id") Long id) throws Exception {
        /** 根据id查询查询简历信息 如果简历信息为空就把简历文件插入到此id的人员 */
        IntervTalent intervTalent = intervTalentService.selectIntervTalentById(id);
            /** 上传文件路径 */
            String filePath = RuoYiConfig.getUploadPath();
            /** 上传并返回新文件名称 */
            String fileName = FileUploadUtils.upload(filePath, file);
            String yuanfilename = file.getOriginalFilename();
            /** 文件访问路径 */
            String url = serverConfig.getUrl() + fileName;
            intervTalent.setFileName(yuanfilename);
            intervTalent.setFilePath(url);
            intervTalentService.updateIntervTalent(intervTalent);
            return AjaxResult.success("上传成功");
        }





    /**
     * 根据id查询多个人才简历文件信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @Log(title = "文件信息", businessType = BusinessType.OTHER)
    @GetMapping("/talents/{id}")
    public AjaxResult selectByIds(@Param("array") @PathVariable Long[] id)
    {
        return AjaxResult.success(intervTalentService.selectIntervTalentByIds(id));
    }












}
